package Entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import Control.MySQLController;

public class User {
	//Attributes
	private String nric;
	private String name;
	private String username;
	private String email;
	private int contact_no;
	private Date dob;
	private String address;
	private char gender;
	private String security_qn;
	private String security_ans;
	private String race_status_code;
	private String user_status_code;
	
	//Zimin attributes
	private String customerUserType;
	
	private String password;
	
	public String getCustomerUserType(){
		return customerUserType;
	}
	
	public void setCustomerUserType(String customerUserType){
		this.customerUserType = customerUserType;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	//Getters and Setters
	public String getNric() {
		return nric;
	}
	public void setNric(String nric) {
		this.nric = nric;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public int getContact_no() {
		return contact_no;
	}
	public void setContact_no(int contact_no) {
		this.contact_no = contact_no;
	}
	public Date getDob() {
		return dob;
	}
	public void setDob(Date dob) {
		this.dob = dob;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public String getSecurity_qn() {
		return security_qn;
	}
	public void setSecurity_qn(String security_qn) {
		this.security_qn = security_qn;
	}
	public String getSecurity_ans() {
		return security_ans;
	}
	public void setSecurity_ans(String security_ans) {
		this.security_ans = security_ans;
	}
	public String getRace_status_code() {
		return race_status_code;
	}
	public void setRace_status_code(String race_status_code) {
		this.race_status_code = race_status_code;
	}
	public String getUser_status_code() {
		return user_status_code;
	}
	public void setUser_status_code(String user_status_code) {
		this.user_status_code = user_status_code;
	}
	
	//Constructors
	public User(){}
	
	//Constructor -- Sherry
	public User(String nric){
		this.nric = nric;
	}
	
	//Constructors -- WR (For registration)
	public User(String nric, String name, String email,
			int contact_no, Date dob, String address, char gender,
			String user_status_code, String password) {
		super();
		this.nric = nric;
		this.name = name;
		this.email = email;
		this.contact_no = contact_no;
		this.dob = dob;
		this.address = address;
		this.gender = gender;
		this.user_status_code = user_status_code;
		this.password = password;
	}
	
	
	//Constructors for ZIMIN {DO NOT TOUCH :} 
	//--Create for Individual 
	public User(String nric, String name, String username, String email,
			int contact_no, Date dob, String address, char gender, String security_qn, 
			String security_ans, String race_status_code,
			 String user_status_code, String password) {
		super();
		this.nric = nric;
		this.name = name;
		this.username = username;
		this.email = email;
		this.contact_no = contact_no;
		this.dob = dob;
		this.address = address;
		this.gender = gender;
		this.security_qn = security_qn; 
		this.security_ans = security_ans;
		this.race_status_code = race_status_code;
		this.user_status_code = user_status_code;
		this.password = password; 
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public ArrayList<User> retrieveAllUsers(){
		ArrayList<User> userList = new ArrayList<User>();
		
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.user";
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				User user = new User();
				user.setNric(rs.getString("nric"));
				user.setName(rs.getString("name"));
				user.setUsername(rs.getString("username"));
				user.setEmail(rs.getString("email"));
				user.setContact_no(rs.getInt("contact_no"));
				user.setDob(rs.getDate("dob"));
				user.setAddress(rs.getString("address"));
				user.setGender(rs.getString("gender").charAt(0));
				user.setSecurity_qn(rs.getString("security_qn"));
				user.setSecurity_ans(rs.getString("security_ans"));
				user.setRace_status_code(rs.getString("race_status_code"));
				user.setUser_status_code(rs.getString("user_status_code"));
				userList.add(user);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return userList;
	}
	
	public boolean retrieveUser(){
		boolean success = false;
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.user WHERE nric = '" + nric + "'";
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				name = rs.getString("name");
				username = rs.getString("username");
				email = rs.getString("email");
				contact_no = rs.getInt("contact_no");
				dob = rs.getDate("dob");
				address = rs.getString("address");
				gender = rs.getString("gender").charAt(0);
				security_qn = rs.getString("security_qn");
				security_ans = rs.getString("security_ans");
				race_status_code = rs.getString("race_status_code");
				user_status_code = rs.getString("user_status_code");
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return success;
	}
	
	public boolean createUserRecord(){
		boolean success = false; 
		MySQLController db = new MySQLController();
		db.getConnection();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String sql = "INSERT INTO ooadpj.user (nric, name, username, password, email, contact_no,dob,address,gender, security_qn, security_ans, race_status_code, user_status_code)";
		sql+="VALUES ('"+this.getNric()+"', '"+this.getName()+"', '"+this.getUsername()+"','"+this.getPassword()+"','"+this.getEmail()+"',"+this.getContact_no()+",'"+sdf.format(this.getDob())+"', '"+this.getAddress()+"', '"+this.getGender()+"', '"+this.getSecurity_qn()+"','"+this.getSecurity_ans()+"', '"+this.getRace_status_code().toUpperCase().charAt(0)+"', 'A' )";  
		System.out.print("second : " +sql);
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	}
	
	
	public boolean updateUserRecord(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "UPDATE ooadpj.user SET email='"+this.getEmail()+"', contact_no=" +this.getContact_no()+ ", address='" +this.getAddress()+ "' WHERE nric='"+this.getNric()+"';";
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	}

	
	public boolean updateUserPassword(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "UPDATE ooadpj.user SET password='"+this.getPassword()+ "' WHERE nric='"+this.getNric()+"';";
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	}
	
	//MIN's CODE - UPDATE STATUS
	public boolean updateUserStatus(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		//Type must be either A or D uppercase
		String sql = "UPDATE ooadpj.user SET user_status_code= 'D' WHERE nric='"+this.getNric()+"';";
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	}
	
	public User retrieveSecQnsAns(String nric){
		User u = null;
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT security_qn,security_ans,contact_no FROM ooadpj.user WHERE nric = '" + nric + "'";
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				this.nric = nric; 
				security_qn = rs.getString("security_qn");
				security_ans = rs.getString("security_ans");
				contact_no = rs.getInt("contact_no");
				u = this;
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return u;
	}
	
	public User loginUser(String uname, String password){
		User u = null;
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.user WHERE username = '" + uname + "' and password = '"+ password +"' and user_status_code='A'";
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				nric = rs.getString("nric");
				name = rs.getString("name");
				username = rs.getString("username");
				email = rs.getString("email");
				contact_no = rs.getInt("contact_no");
				dob = rs.getDate("dob");
				address = rs.getString("address");
				gender = rs.getString("gender").charAt(0);
				security_qn = rs.getString("security_qn");
				security_ans = rs.getString("security_ans");
				race_status_code = rs.getString("race_status_code");
				user_status_code = rs.getString("user_status_code");
				this.password = rs.getString("password");
				u = new User(nric, name, username, email, contact_no, dob, address, gender, security_qn, security_ans, race_status_code, user_status_code, password);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return u;
	}
	
	
	public User checkMember(String uname){
		User u = null;
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.user WHERE username = '" + uname + "' and user_status_code='A'";
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				nric = rs.getString("nric");
				name = rs.getString("name");
				username = rs.getString("username");
				email = rs.getString("email");
				contact_no = rs.getInt("contact_no");
				dob = rs.getDate("dob");
				address = rs.getString("address");
				gender = rs.getString("gender").charAt(0);
				security_qn = rs.getString("security_qn");
				security_ans = rs.getString("security_ans");
				race_status_code = rs.getString("race_status_code");
				user_status_code = rs.getString("user_status_code");
				
				u = new User(nric, name, username, email, contact_no, dob, address, gender, security_qn, security_ans, race_status_code, user_status_code, password);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return u;
	}
	
	public String validateUserType(){
		String userType = null;
		
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		
		String sql = "SELECT COUNT(*) AS 'NEEDY' FROM ooadpj.user INNER JOIN ooadpj.needy_resident ON user.nric = needy_resident.nric WHERE user.nric = '" + nric + "'";
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				if(rs.getInt("NEEDY") == 1){
					userType = "Needy";
					db.terminate();
					return userType;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();

		db.getConnection();
		sql = "SELECT type, COUNT(*) AS 'CUSTOMER' FROM ooadpj.user INNER JOIN ooadpj.customer ON user.nric = customer.nric WHERE user.nric = '" + nric + "'";
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				if(rs.getInt("CUSTOMER") == 1){
					userType = "Customer";
					customerUserType = rs.getString("type");
					db.terminate();
					return userType;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();
		
		userType = "Staff";
		return userType;
	}
}
